package com.example.graduation.controller.company;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.graduation.entity.pojo.Company;
import com.example.graduation.service.CompanyService;
import com.example.graduation.util.ShiroUtil;
import com.example.graduation.util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @author Jeffery
 * Job权限的Controller
 */
@RequestMapping("/company")
@Controller
@Slf4j(topic = "CompanyController")
@Api(tags = "带有company路径的controller")
public class CompanyController {
    /**
     * 页面显示数据条数默认为5
     */
    private static final Integer PAGE_SIZE = 5;

    @Resource
    private CompanyService companyService;

    /**
     * 添加公司
     *
     * @return 页面跳转
     */
    @PostMapping("/saveCompany")
    @ApiOperation("添加公司")
    public String saveCompany(@Validated @RequestParam("companyName") String companyName,
                              @Validated @RequestParam("companyDescription") String companyDescription,
                              @Validated @RequestParam("companyEmail") String companyEmail,
                              @Validated @RequestParam("companyAddressId") String companyAddressId,
                              Model model) {
        Company company = new Company();

        company.setId(UUID.randomUUID().toString());
        company.setName(companyName);
        company.setDescription(companyDescription);
        company.setEmail(companyEmail);
        company.setAddressId(companyAddressId);

        company.setCreateTime(new Timestamp(System.currentTimeMillis()));

        companyService.save(company);
        log.info("添加公司成功 : {}", company.getId());
        model.addAttribute("email", ShiroUtil.getUserId());
        return "index";
    }

    /**
     * 获取所有公司（分页）
     *
     * @param pageNumber 当前页数
     * @return json
     */
    @PostMapping("/getAllCompany/{pageNumber}")
    @ResponseBody
    @ApiOperation("获取所有公司（分页）")
    @ApiImplicitParam(name = "pageNumber", value = "当前页数", defaultValue = "1")
    public String getAllCompany(@PathVariable("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        List<Company> companyList = companyService.list();
        PageInfo<Company> pageInfo = new PageInfo<>(companyList);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 获取一个公司
     *
     * @param companyId 公司ID
     * @return html
     */
    @GetMapping("/getOneCompany/{companyId}")
    @ApiOperation("获取一个公司")
    @ApiImplicitParam(name = "companyId", value = "公司ID", defaultValue = "")
    public String getOneCompany(@PathVariable("companyId") String companyId,
                                Model model) {
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id", companyId);
        Company company = companyService.getOne(wrapper);
        model.addAttribute("company", company);

        return "";
    }
}
